Skip to content

Fix degenerate MultiPolygon centroids - #7921

Merged
camdecoster merged 5 commits into
plotly:masterfrom
swjturay:agent/fix-degenerate-geojson-centroid
Jul 31, 2026
Merged

Fix degenerate MultiPolygon centroids#7921
camdecoster merged 5 commits into
plotly:masterfrom
swjturay:agent/fix-degenerate-geojson-centroid

Conversation

@swjturay

Copy link
Copy Markdown

Overview

Fixes #7874.

Prevent a zero-area MultiPolygon feature from aborting choropleth rendering. When centroid selection cannot find a positive-area sub-polygon, the feature now receives the existing [NaN, NaN] centroid sentinel. Its geometry remains available for rendering, while valid sibling features continue through extraction.

Root cause and user impact

The MultiPolygon centroid path initialized maxArea to zero and only selected sub-polygons whose area was strictly greater. If every sub-polygon had zero area, the selected polygon remained undefined and was passed to Turf's centroid helper, causing an uncaught TypeError that stopped the entire plot.

The guard is limited to that no-positive-area case. Centroid calculation for valid Polygon and MultiPolygon features is unchanged.

Validation

  • Added a regression test using the issue's two-distinct-vertex degenerate ring alongside a valid polygon.
  • Verified the regression test failed before the source change with Cannot read properties of undefined (reading 'type').
  • npm run test-jasmine -- lib_geo_location_utils choropleth --nowatch --report-spec — 37/37 passed (Chromium-based Edge via CHROME_BIN).
  • npm run test-syntax
  • npm run lint
  • git diff --check

@swjturay
swjturay marked this pull request as ready for review July 26, 2026 13:01
@robertclaus
robertclaus requested a review from camdecoster July 27, 2026 19:15
@camdecoster

Copy link
Copy Markdown
Contributor

Thanks for the PR! I'll take a look and follow up.

@camdecoster camdecoster self-assigned this Jul 28, 2026

@camdecoster camdecoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Your solution was spot on. While reviewing, I noticed another potential failure mode where polygon rings could have no points in the array so I added a guard against that as well (along with some more tests). I also added some logging similar to what we already do for invalid GeoJSON geometry.

@camdecoster
camdecoster merged commit b074563 into plotly:master Jul 31, 2026
85 of 86 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: choropleth with custom GeoJSON: a single malformed feature throws an uncaught TypeError and blanks the entire plot

2 participants